# -*- coding: utf-8 -*-
# coding:unicode_escape


import time
import math

from parking_lot.parking import ParkingLot, Parking


class ParkingManageSystem(object):
    """
    定义停车管理系统类，可以停车、查询车辆、取车、收费
    """

    def __init__(self, parking_info_list, parking_strategy):
        super(ParkingManageSystem, self).__init__()
        self.unit_price_per_hour = 6
        self.parking_info_list = parking_info_list
        self.parking_strategy = parking_strategy

    def charges(self, hours):
        """
        收费系统，不满1小时按1小时收费
        param1: 停车小时数
        return: 返回停车费用
        """
        
        if hours <= 0:
            return False
        
        costs = hours * self.unit_price_per_hour
        return costs

    def park(self, plate_number):
        """
        按停车策略执行停车
        """
        
        parking = Parking(self.parking_info_list, plate_number, self.parking_strategy)

    def pick_up(self, plate_number):
        """
        取车
        param1: 取车车牌号
        """
        
        parking_info_list_index, starttime = self.query_car(plate_number)
        if not starttime:
            print("没有查询到车牌号为 %s 的车辆，请联系管理员。" % (plate_number,))
            return False
        
        stoptime = time.time()
        park_hours = (stoptime - starttime)/3600
        park_hours = math.ceil(park_hours)
        costs = self.charges(park_hours)
        print("收费: %d 元。谢谢！再见！" % (costs,))
        self.remove_car(parking_info_list_index, plate_number)

    def query_car(self, plate_number):
        """
        查询车辆
        param1: 要查询的车辆车牌号
        return: 返回车辆所在的停车场序号、开始停入时间
        """
        
        for index in range(0, len(self.parking_info_list)):
            for car in self.parking_info_list[index][2]:
                if plate_number == car.get_plate_number():
                    print("%s 停入停车场 %s 的时间为 %s" % (plate_number, self.parking_info_list[index][0], time.ctime(car.get_starttime())))
                    return index, car.get_starttime()

    def remove_car(self, index, plate_number):
        """
        从停车场移除已经取走的车辆信息
        param1: 车辆所在的停车场序号
        param2: 要移除的车辆车牌号
        """
        
        for car in self.parking_info_list[index][2]:
            if plate_number == car.get_plate_number():
                self.parking_info_list[index][2].remove(car)
                return True
